<?php

class Ext_Model_Resource extends Ext_Db_Table
{

    protected $_name = 'sys_resource';

    protected $_primary = 'resource_id';

    protected $_sequence = 'sys_resource_id_seq';

    protected $_referenceMap = array(
		'Acls' => array(
	       	'columns' => array( 'resource_id' ),
	        'refTableClass' => 'Ext_Model_AclRule',
			'refColumns' => array( 'resource_id' )
		)
    );

    public function getRoleByResource( $userId, $appId )
    {
    	$query = $this->getAdapter()
    		->select()
    		->from( array( 're'=> 'sys_resource' ), array() )
    		->distinct( 'ro.role_id' )
    		->join ( array ( 'ar' => 'sys_acl_rule' ), 'ar.resource_id = re.resource_id', array('') )
    		->join ( array ( 'ur' => 'sys_user_role' ), 'ur.role_id = ar.role_id', array( '' ) )
    		->join ( array ( 'ro' => 'sys_role' ), 'ro.role_id = ur.role_id', array( '*' ) )
    		->where( 'ur.user_id = ?', $userId )
    		->where( 're.application_id = ?', $appId );
    		
    	$rowset = $this->getAdapter()->fetchAll( $query );
    	
    	
    	if( count( $rowset )) {
    		return $rowset;
    	}
    	return array();
    	
    }

    public function getAclByResource( $userId, $appId )
    {
    	$query = $this->getAdapter()
    		->select()
    		->from( array( 're'=> 'sys_resource' ), array( 'resource_name' => 're.name' ) )
    		->join ( array ( 'ar' => 'sys_acl_rule' ), 'ar.resource_id = re.resource_id', array('*') )
    		->join ( array ( 'ur' => 'sys_user_role' ), 'ur.role_id = ar.role_id', array( '' ) )
    		->join ( array ( 'ro' => 'sys_role' ), 'ro.role_id = ur.role_id', array( 'role_name' => 'name' ) )
    		->where( 'ur.user_id = ?', $userId )
    		->where( 're.application_id = ?', $appId );
    		
    	$rowset = $this->getAdapter()->fetchAll( $query );
    	
    	
    	if( count( $rowset )) {
    		return $rowset;
    	}
    	return array();
    }

    public function getResourceList( $appId )
    {
		if( empty( $appId )){
			return array();
		}
		
		$rowset = $this->fetchAll( 
			$this->select()
			->where('application_id = ?', $appId ) 
		);
		if( count( $rowset )) {
			return $rowset->toArray();
		}
		return array();
    }
    
    
	public function getResourceAsQuery()
	{
		return $this->getAdapter()->select()
			->from( array('re'=>'sys_resource'), array('*') )
			->join( array( 'app'=>'sys_application' ), 'app.application_id = re.application_id', array('app_name'=>'name' ) )
			->order( 'app.name')
			->order( 're.name');		
	}
    public function getLov( ) {

        $rowSet = $this->getAdapter()->fetchAll( 
        	$this->getAdapter()->select()
			->from( array('re'=>'sys_resource'), array('*') )
			->join( array( 'app'=>'sys_application' ), 'app.application_id = re.application_id', array('app_name'=>'name' ) )
			->order( 'app.name')
			->order( 're.name') 
        );

        $ret = array();
        foreach ( $rowSet as $row ) {
            $ret[ $row['resource_id'] ]= $row['name'] . ' / ' .$row['app_name'];
        }
        return $ret;
    }


    public function getResourceId( $resourceName, $appId )
    {
        $select = $this->select()
        	->where( 'name = ?', $resourceName )
        	->where( 'application_id = ?', $appId );
        $result = $this->fetchRow( $select );
        if ( empty( $result ) ) {
            return null;
        }
        return $result->resource_id;
    }
}
